package com.onesports.intelligent.k12.polarlight.controller.organization.report;

import cn.gjing.excel.executor.ExcelFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onesports.framework.kit.common.valid.Json;
import com.onesports.intelligent.k12.polarlight.common.utils.ResultUtil;
import com.onesports.intelligent.k12.polarlight.constant.ApiPathConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.operation.PageOrderDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.organization.OrgPageOrderDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.report.OrganizationAccountantQueryDTO;
import com.onesports.intelligent.k12.polarlight.domain.vo.market.TotalDataVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.order.OrderPageVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.report.OrganizationAccountantVO;
import com.onesports.intelligent.k12.polarlight.enums.OrderStatusEnum;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import com.onesports.intelligent.k12.polarlight.service.order.OrderService;
import com.onesports.intelligent.k12.polarlight.service.report.AccountantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "机构端-财务管理-收支统计")
@RestController
@RequiredArgsConstructor
@RequestMapping(ApiPathConstants.ORGANIZATION_ACCOUNTANT)
public class OrganizationAccountantController {

    private final AccountantService accountantService;

    private final OrderService orderService;

    @ApiOperation("查询")
    @GetMapping("/page")
    public ResponseEntity<TotalDataVO<OrganizationAccountantVO>> page(@Json OrganizationAccountantQueryDTO dto){
        return ResultUtil.success(accountantService.selectOrganizationPage(dto, SecurityUtils.getOrganizationId()));
    }

    @ApiOperation("导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response, OrgPageOrderDTO dto) {
        List<OrganizationAccountantVO> data = accountantService.organizationExport(dto,SecurityUtils.getOrganizationId());
        ExcelFactory.createWriter(OrganizationAccountantVO.class, response)
                .write(data).flush();
    }

    @ApiOperation("查询订单列表")
    @GetMapping("/order-page")
    public ResponseEntity<TotalDataVO<OrderPageVO>> selectOrderPageTotal(@Json OrganizationAccountantQueryDTO dto){
        PageOrderDTO pageOrderDTO = new PageOrderDTO();
        pageOrderDTO.setOrderStatus(OrderStatusEnum.ALREADY.getValue());
        pageOrderDTO.setCampusId(dto.getCampusId());
        pageOrderDTO.setOrganizationId(SecurityUtils.getOrganizationId());
        pageOrderDTO.setPayTimeStart(dto.getOrderStartTime());
        pageOrderDTO.setPayTimeEnd(dto.getOrderEndTime());
        return ResultUtil.success(orderService.selectPageTotal(new Page<OrderPageVO>(dto.getCurrPage(),dto.getSize()),pageOrderDTO));
    }
}
